package com.c1202m.dao;

import com.c1202m.gui.BangThongKe;
import com.c1202m.util.DateUtil;
import com.c1202m.gui.CustomMessageDialog;
import com.c1202m.hibernate.entities.RutTien;
import com.c1202m.hibernate.entities.TaiKhoan;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.hibernate.Query;
import org.hibernate.Session;

public class RutTienDAO {

    public static Vector layToanBoDoiTuong() {
        Vector vt = new Vector();
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("select r.ngayRut, t.chuTK, t.soCMND, r.soTK, r.soTienRut from RutTien r inner join r.taiKhoan t");
            list = query.list();
            for (Iterator it = list.iterator(); it.hasNext();) {
                Object[] o = (Object[]) it.next();
                Vector temp = new Vector();
                temp.add(o[0]);
                temp.add(o[1]);
                temp.add(o[2]);
                temp.add(o[3]);
                temp.add(o[4]);
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static Vector layDoiTuongTheoSoTk(String soTK) {
        Vector vt = new Vector();
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("from RutTien a where a.soTK = ?");
            query.setString(0, soTK);
            list = query.list();
            for (Object obj : list) {
                RutTien rutTien = (RutTien) obj;
                Vector temp = new Vector();
                temp.add(rutTien.getNgayRut());
                temp.add(rutTien.getSoTK());
                temp.add(rutTien.getSoTienRut());
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static Vector layDoiTuongTheoNgay() {
        Vector vt = new Vector();
        if (BangThongKe.jxDate.getDate() == null) {
            return vt;
        }
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("select r.ngayRut, t.chuTK, t.soCMND, r.soTK, r.soTienRut from RutTien r inner join r.taiKhoan t where r.ngayRut between ? and ?");

            Date ngayRut = BangThongKe.jxDate.getDate();
            java.sql.Timestamp sqlDateFirstTime = new java.sql.Timestamp(DateUtil.getDayFirstTime(ngayRut).getTime());
            java.sql.Timestamp sqlDateLastTime = new java.sql.Timestamp(DateUtil.getDayLastTime(ngayRut).getTime());
            String strF = String.valueOf(sqlDateFirstTime);
            String strL = String.valueOf(sqlDateLastTime);

            query.setString(0, strF);
            query.setString(1, strL);
            list = query.list();
            for (Iterator it = list.iterator(); it.hasNext();) {
                Object[] o = (Object[]) it.next();
                Vector temp = new Vector();
                temp.add(o[0]);
                temp.add(o[1]);
                temp.add(o[2]);
                temp.add(o[3]);
                temp.add(o[4]);
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static int soGiaoDichTrongNgay(String soTK, Date ngay) {
        int soLanGD = 0;
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("from RutTien a where a.soTK = ? and a.ngayRut between ? and ?");
            java.sql.Timestamp sqlDateFirstTime = new java.sql.Timestamp(DateUtil.getDayFirstTime(ngay).getTime());
            java.sql.Timestamp sqlDateLastTime = new java.sql.Timestamp(DateUtil.getDayLastTime(ngay).getTime());
            String strF = String.valueOf(sqlDateFirstTime);
            String strL = String.valueOf(sqlDateLastTime);
            query.setString(0, soTK);
            query.setString(1, strF);
            query.setString(2, strL);
            list = query.list();
            soLanGD = list.size();
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return soLanGD;
    }

    public static double soTienRutTrongNgay(String soTK, Date ngay) {
        double soTien = 0;
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("select r.soTienRut from RutTien r where r.soTK = ? and r.ngayRut between ? and ?");
            java.sql.Timestamp sqlDateFirstTime = new java.sql.Timestamp(DateUtil.getDayFirstTime(ngay).getTime());
            java.sql.Timestamp sqlDateLastTime = new java.sql.Timestamp(DateUtil.getDayLastTime(ngay).getTime());
            String strF = String.valueOf(sqlDateFirstTime);
            String strL = String.valueOf(sqlDateLastTime);
            query.setString(0, soTK);
            query.setString(1, strF);
            query.setString(2, strL);
            list = query.list();
            for (Iterator it = list.iterator(); it.hasNext();) {
                soTien = soTien + Double.valueOf(it.next().toString());
                System.out.println(soTien);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }

        return soTien;
    }

    public static boolean rutTien(String soTK, double soTienRut) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            TaiKhoan taiKhoan = (TaiKhoan) session.get(TaiKhoan.class, soTK);
            double soDuTK = Double.valueOf(taiKhoan.getSoDuTK()) - Double.valueOf(soTienRut);
            taiKhoan.setSoDuTK(soDuTK);
            session.update(taiKhoan);
            RutTien rutTien = new RutTien();
            rutTien.setNgayRut(new Date());
            rutTien.setSoTK(soTK);
            rutTien.setSoTienRut(soTienRut);
            session.save(rutTien);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }
}
